import request from '@/utils/request'

import type { ResponseRoleType, ResponseRolePermission } from './type'

enum Api {
  allRoleList = '/admin/acl/role/',
  // 新增
  addRole = '/admin/acl/role/save',
  // 更新
  updateRole = '/admin/acl/role/update',
  // 全部菜单权限
  allPermission = '/admin/acl/permission/toAssign/',
  // 分配权限
  setRolePermission = '/admin/acl/permission/doAssign/?',
  // 删除角色
  deleteCurRole = '/admin/acl/role/remove/',
}

export const reqRoleList = (
  pageNo: number,
  pageSize: number,
  roleName: string,
) => {
  return request.get<any, ResponseRoleType>(
    Api.allRoleList + `${pageNo}/${pageSize}/?roleName=${roleName}`,
  )
}

export const addOrUpdateRole = (data: any) => {
  if (data.id) return request.put<any, any>(Api.updateRole, data)
  return request.post<any, any>(Api.addRole, data)
}

export const reqAllRolePermission = (id: number) => {
  return request.get<any, ResponseRolePermission>(Api.allPermission + id)
}

export const reqSetRolePermission = (
  roleId: number,
  permissionId: number[],
) => {
  return request.post<any, any>(
    Api.setRolePermission + `roleId=${roleId}&permissionId=${permissionId}`,
  )
}

export const reqDeleteRole = (id: number) => {
  return request.delete<any, any>(Api.deleteCurRole + id)
}
